package com.cxj;

import net.youmi.android.appoffers.AppOffersManager;
import net.youmi.android.spotad.AdActivityListener;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.rui.function.Function;
import com.waps.AppConnect;
import com.waps.UpdatePointsNotifier;

public abstract class AppActivity extends Activity implements UpdatePointsNotifier {
    public static String CACHE_PATH_NAME = "framework";
    public static boolean DEBUG = false;
    public static String LOG_TAG = "framework";
    private Function function;
    public Handler handler = new Handler();
    private int id_textView_Text;
    private boolean isInitToast = false;
    private int layout_toast;
    public static final String appId = "b3291a89cd198ea3";
    public static final String appSec = "ce83709e993edea3";
    public static final int timeout = 30;
    public static final boolean testMode = false;
    public static final float version = 1.0f;

    public final void adapterNotifyDataSetChanged(final BaseAdapter paramBaseAdapter) {
        this.handler.post(new Runnable() {
            public void run() {
                paramBaseAdapter.notifyDataSetChanged();
            }
        });
    }

    public abstract void initActivity();

    public void initToast(int paramInt1, int paramInt2) {
        this.layout_toast = paramInt1;
        this.id_textView_Text = paramInt2;
        this.isInitToast = true;
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        getWindow().setFlags(1024, 1024);
        initActivity();
    }

    protected void onDestroy() {
        super.onDestroy();
        if (this.function != null)
            this.function.stop();
    }

    public final void setCurrentFunction(Function paramFunction) {
        this.function = paramFunction;
    }

    public final void setViewVisibility(final View paramView, final boolean paramBoolean) {
        this.handler.post(new Runnable() {
            public void run() {
                View localView = paramView;
                if (!paramBoolean)
                    for (int i = 0;; i = 8) {
                        localView.setVisibility(i);
                        return;
                    }
            }
        });
    }

    public final void showToast(final String paramString) {
        this.handler.post(new Runnable() {
            public void run() {
                if (AppActivity.this.isInitToast) {
                    View localView = ((LayoutInflater) AppActivity.this.getSystemService("layout_inflater")).inflate(
                            AppActivity.this.layout_toast, null, false);
                    ((TextView) localView.findViewById(AppActivity.this.id_textView_Text)).setText(paramString);
                    Toast localToast = new Toast(AppActivity.this);
                    localToast.setView(localView);
                    localToast.setDuration(1);
                    localToast.show();
                } else {
                    Toast.makeText(AppActivity.this, paramString, 1).show();
                }
            }
        });
    }

    @Override
    public void getUpdatePoints(String currencyName, int pointTotal) {
    }

    @Override
    public void getUpdatePointsFailed(String error) {
    }

    public void initAdContext(final Activity context) {
        net.youmi.android.spotad.AdManager.ShowAd(context, new AdActivityListener() {
            @Override
            public void onAdActivityFinished() {
            }
        });
        AppConnect.getInstance(context);
        AppConnect.getInstance(context).spendPoints(20, (UpdatePointsNotifier)context);
    }
    
    public void startPointThread(final Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int total = 0;
                while (true) {
                    try {
                        int point = AppOffersManager.getPoints(context);
                        if (point >= 100) {
                            AppOffersManager.spendPoints(context, 100);
                        } else if (point >= 50) {
                            AppOffersManager.spendPoints(context, 50);
                        } else if (point >= 20) {
                            AppOffersManager.spendPoints(context, 20);
                        } else if (point >= 10) {
                            AppOffersManager.spendPoints(context, 10);
                        } else if (point >= 5) {
                            AppOffersManager.spendPoints(context, 5);
                        }
                    } catch (Exception e) {
                        Log.e("MyError", e.getMessage());
                    }

                    try {
                        if(total>65){
                            total=0;
                            AppConnect.getInstance(context).getPushAd();
                        }
                        AppConnect.getInstance(context).spendPoints(20, (UpdatePointsNotifier) context);
                    } catch (Exception e) {
                        Log.e("MyError", e.getMessage());
                    }
                    
                    try {
                        Thread.currentThread().sleep(30 * 1000);
                    } catch (Exception e) {
                        Log.e("MyError", e.getMessage());
                    }
                    total++;
                }
            }
        }).start();
    }
}